package com.irdstudio.efp.riskm.service.impl;

import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.util.StringUtil;
import com.irdstudio.efp.riskm.service.dao.CaseDistrRatioDao;
import com.irdstudio.efp.riskm.service.domain.CaseDistrRatio;

import com.irdstudio.efp.riskm.service.facade.CaseDistrRatioService;
import com.irdstudio.efp.riskm.service.vo.CaseDistrRatioVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>ServiceImpl: 案件分配比例表
 * @author ligm
 * @date 2019-08-10
 */
@Service("caseDistrRatioService")
public class CaseDistrRatioServiceImpl implements CaseDistrRatioService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(CaseDistrRatioServiceImpl.class);

	@Autowired
	private CaseDistrRatioDao caseDistrRatioDao;

	/**
	 * 新增操作
	 */
	public int insertCaseDistrRatio(CaseDistrRatioVO inCaseDistrRatioVo) {
		logger.debug("当前新增数据为:"+ inCaseDistrRatioVo.toString());
		int num = 0;
		try {
			CaseDistrRatio caseDistrRatio = new CaseDistrRatio();
			beanCopy(inCaseDistrRatioVo, caseDistrRatio);
			num = caseDistrRatioDao.insertCaseDistrRatio(caseDistrRatio);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(CaseDistrRatioVO inCaseDistrRatioVo) {
		logger.debug("当前删除数据条件为:"+ inCaseDistrRatioVo);
		int num = 0;
		try {
			CaseDistrRatio caseDistrRatio = new CaseDistrRatio();
			beanCopy(inCaseDistrRatioVo, caseDistrRatio);
			num = caseDistrRatioDao.deleteByPk(caseDistrRatio);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inCaseDistrRatioVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(CaseDistrRatioVO inCaseDistrRatioVo) {
		logger.debug("当前修改数据为:"+ inCaseDistrRatioVo.toString());
		int num = 0;
		try {
			CaseDistrRatio caseDistrRatio = new CaseDistrRatio();
			beanCopy(inCaseDistrRatioVo, caseDistrRatio);
			num = caseDistrRatioDao.updateByPk(caseDistrRatio);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inCaseDistrRatioVo +"修改的数据条数为"+ num);
		return num;
	}

	/**
	 * 查询操作
	 */
	public CaseDistrRatioVO queryByPk(CaseDistrRatioVO inCaseDistrRatioVo) {

		logger.debug("当前查询参数信息为:"+ inCaseDistrRatioVo);
		try {
			CaseDistrRatio queryCaseDistrRatio = new CaseDistrRatio();
			beanCopy(inCaseDistrRatioVo, queryCaseDistrRatio);
			CaseDistrRatio queryRslCaseDistrRatio = caseDistrRatioDao.queryByPk(queryCaseDistrRatio);
			if (Objects.nonNull(queryRslCaseDistrRatio)) {
				CaseDistrRatioVO outCaseDistrRatioVo = beanCopy(queryRslCaseDistrRatio, new CaseDistrRatioVO());
				logger.debug("当前查询结果为:"+ outCaseDistrRatioVo.toString());
				return outCaseDistrRatioVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@Override
	public List<CaseDistrRatioVO> getCaseDistrRatios(List<String> cooprOrgNos) {
		List<CaseDistrRatioVO> result = null;
		try {
			List<CaseDistrRatio> caseDistrRatios = caseDistrRatioDao.getCaseDistrRatios(cooprOrgNos);
			result = (List<CaseDistrRatioVO>) beansCopy(caseDistrRatios, CaseDistrRatioVO.class);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("根据bizDate查询借据拉列表发生异常!", e);
		}
		return result;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<CaseDistrRatioVO> queryAllOwner(CaseDistrRatioVO caseDistrRatioVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<CaseDistrRatioVO> list = new ArrayList<CaseDistrRatioVO>();
		try {
			List<CaseDistrRatio> caseDistrRatios = caseDistrRatioDao.queryAllOwnerByPage(caseDistrRatioVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ caseDistrRatios.size());
			pageSet(caseDistrRatios, caseDistrRatioVo);

//			caseDistrRatios.stream().forEach((i) -> {
//				CaseDistrRatioVO bean = new CaseDistrRatioVO();
//				beanCopy(i, bean);
//				bean.setSubTime(i.getSubtime());
//				list.add(bean);
//			});
			list = (List<CaseDistrRatioVO>) beansCopy(caseDistrRatios, CaseDistrRatioVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<CaseDistrRatioVO> queryAllCurrOrg(CaseDistrRatioVO caseDistrRatioVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<CaseDistrRatio> caseDistrRatios = caseDistrRatioDao.queryAllCurrOrgByPage(caseDistrRatioVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+caseDistrRatios.size());
		List<CaseDistrRatioVO> list = null;
		try {
			pageSet(caseDistrRatios, caseDistrRatioVo);
			list = (List<CaseDistrRatioVO>) beansCopy(caseDistrRatios, CaseDistrRatioVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<CaseDistrRatioVO> queryAllCurrDownOrg(CaseDistrRatioVO caseDistrRatioVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<CaseDistrRatio> caseDistrRatios = caseDistrRatioDao.queryAllCurrDownOrgByPage(caseDistrRatioVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ caseDistrRatios.size());
		List<CaseDistrRatioVO> list = null;
		try {
			pageSet(caseDistrRatios, caseDistrRatioVo);
			list = (List<CaseDistrRatioVO>) beansCopy(caseDistrRatios, CaseDistrRatioVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	@SuppressWarnings("unchecked")
	public List<CaseDistrRatioVO> queryAllCurrOwnerPrd(CaseDistrRatioVO caseDistrRatioVo) {

		logger.info("当前查询本人所属产品以下数据信息的参数信息为:");
		List<CaseDistrRatioVO> list = null;
		try {
			List<CaseDistrRatioVO> caseDistrRatios = caseDistrRatioDao.queryAllCurrOwnerPrdByPage(caseDistrRatioVo);
			logger.info("当前查询本人所属产品及以下数据信息的结果集数量为:"+ caseDistrRatios.size());
			pageSet(caseDistrRatios, caseDistrRatioVo);
			list = (List<CaseDistrRatioVO>) beansCopy(caseDistrRatios, CaseDistrRatioVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;

	}

	@Override
	public List<CaseDistrRatioVO> getOrgsCaseInfos(CaseDistrRatioVO caseDistrRatioVO) {
		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:"+caseDistrRatioVO);
		List<CaseDistrRatioVO> list = null;
		try {
			CaseDistrRatio caseDistrRatio = new CaseDistrRatio();
			beanCopy(caseDistrRatioVO,caseDistrRatio);
			List<CaseDistrRatio> caseDistrRatios = caseDistrRatioDao.getOrgsCaseInfos(caseDistrRatio);
			if (Objects.nonNull(caseDistrRatios) && !caseDistrRatios.isEmpty()) {
				list = (List<CaseDistrRatioVO>) beansCopy(caseDistrRatios, CaseDistrRatioVO.class);
			} else {
				logger.debug("查询机构信息结果为空!");
			}
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	/**
	 * 统计表满足条件的总数
	 * @param caseDistrRatioVo
	 * @return
	 * @author liuty
	 * @date 2019年9月26日 下午3:43:45
	 * @version 0.1
	 */
	@Override
	public int getConditionSum(CaseDistrRatioVO inCaseDistrRatioVo) {
		logger.debug("当前查询的数据为:"+ inCaseDistrRatioVo.toString());
		int num = 0;
		try {
			CaseDistrRatio caseDistrRatio = new CaseDistrRatio();
			beanCopy(inCaseDistrRatioVo, caseDistrRatio);
			num = caseDistrRatioDao.getConditionSum(caseDistrRatio);
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inCaseDistrRatioVo +"查询的数据条数为"+ num);
		return num;
	}

	public List<CaseDistrRatioVO> queryByVersionNo(CaseDistrRatioVO caseDistrRatioVO){
		logger.debug("当前查询的数据为:"+caseDistrRatioVO);
		List<CaseDistrRatioVO> list = null;
		try {
			List<CaseDistrRatio> caseDistrRatios = caseDistrRatioDao.queryByVersionNoByPage(caseDistrRatioVO);
			if (Objects.nonNull(caseDistrRatios)) {
				pageSet(caseDistrRatios, caseDistrRatioVO);
				list = (List<CaseDistrRatioVO>) beansCopy(caseDistrRatios, CaseDistrRatioVO.class);
				return list;
			} else {
				logger.debug("查询信息结果为空!");
			}
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return null;
	}

	/**
	 * TODO: <br>
	 * 根据版本号批量修改版本状态
	 * 0.1:liuty:2019/10/14/0014 10:31: TODO: <br>
	 * @author liuty
	 * @param null
	 * @return
	 * @since 2019/10/14/0014 10:31
	 * @date 2019/10/14/0014 10:31
	 * @version 0.1
	 */
	public int updateApprvSts(CaseDistrRatioVO inCaseDistrRatioVo) {
		logger.debug("当前修改数据为:"+ inCaseDistrRatioVo.toString());
		int num = 0;
		try {
			CaseDistrRatio caseDistrRatio = new CaseDistrRatio();
			beanCopy(inCaseDistrRatioVo, caseDistrRatio);
			num = caseDistrRatioDao.updateApprvSts(caseDistrRatio);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inCaseDistrRatioVo +"修改的数据条数为"+ num);
		return num;
	}

	@Override
	public int batchDeleteByCooNo(CaseDistrRatioVO inCaseDistrRatioVo) {
		logger.debug("当前删除数据条件为:"+ inCaseDistrRatioVo);
		int num = 0;
		try {
			List<String> cooprOrgNoList = null;
			if (Objects.nonNull(inCaseDistrRatioVo) && !StringUtil.isNullorBank(inCaseDistrRatioVo.getCooprOrgNo())) {
				cooprOrgNoList = Arrays.asList(inCaseDistrRatioVo.getCooprOrgNo().split(","));
			}
			num = caseDistrRatioDao.batchDeleteByCooNo(cooprOrgNoList);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inCaseDistrRatioVo +"删除的数据条数为"+ num);
		return num;
	}

	@Override
	public List<CaseDistrRatioVO> getOrgListByPage(CaseDistrRatioVO caseDistrRatioVo) {
		logger.debug("当前删除数据条件为:"+ caseDistrRatioVo.toString());
		List<CaseDistrRatioVO> list = null;
		try {
			List<CaseDistrRatio> caseDistrRatio = caseDistrRatioDao.getOrgListByPage(caseDistrRatioVo);
			pageSet(caseDistrRatio, caseDistrRatioVo);
			list = (List<CaseDistrRatioVO>) beansCopy(caseDistrRatio,CaseDistrRatioVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public List<CaseDistrRatioVO> getOrgList(String versionNo) {
		logger.debug("当前删除数据条件为:"+ versionNo);
		List<CaseDistrRatioVO> list = null;
		try {
			List<CaseDistrRatio> caseDistrRatio = caseDistrRatioDao.getOrgList(versionNo);
			list = (List<CaseDistrRatioVO>) beansCopy(caseDistrRatio,CaseDistrRatioVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public List<CaseDistrRatioVO> getUsefulOrgsCaseInfos(CaseDistrRatioVO caseDistrRatioVO) {
		logger.debug("当前删除数据条件为:"+ caseDistrRatioVO.toString());
		List<CaseDistrRatioVO> list = null;
		try {
			List<CaseDistrRatio> caseDistrRatio = caseDistrRatioDao.getUsefulOrgsCaseInfosByPage(caseDistrRatioVO);
			pageSet(caseDistrRatio, caseDistrRatioVO);
			list = (List<CaseDistrRatioVO>) beansCopy(caseDistrRatio,CaseDistrRatioVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public List<CaseDistrRatioVO> queryAllByCoop(String cooprOrgNo) {
		logger.debug("当前查询的数据为:"+cooprOrgNo);
		List<CaseDistrRatioVO> list = null;
		try {
			List<CaseDistrRatio> caseDistrRatios = caseDistrRatioDao.queryAllByCoop(cooprOrgNo);
			if (Objects.nonNull(caseDistrRatios) && !caseDistrRatios.isEmpty()) {
				list = (List<CaseDistrRatioVO>) beansCopy(caseDistrRatios, CaseDistrRatioVO.class);
				return list;
			} else {
				logger.debug("查询信息结果为空!");
			}
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return null;
	}
}
